回溯法之无优先级运算问题代码

#include "iostream.h"
#include "stdio.h"
int n; //给定数字的个数
int a[999];//给定的数字
int m;//利用给定的数字 运算求出的数字m
int num[999];
int oper[999];
int flag[999];
int k;

found()
{
 int x=num[0];
 for(int i=0;i<k;i++)
    {
     switch(oper[i])
        {
         case 0: x+=num[i+1];break;
         case 1: x-=num[i+1];break;
         case 2: x*=num[i+1];break;
         case 3: x/=num[i+1];break;
        }
     }
 return(x==m);
}

search(int dep)
{
 if(dep>k)
   {
    if(found())
    return true;
    else
    return false;
   }
 for(int i=0;i<n;i++)
    if(flag[i]==0)
       {
        num[dep]=a[i];
        flag[i]=1;
        for(int j=0;j<4;j++)
          {
           oper[dep]=j;
           if(search(dep+1))
              return true;
          }
        flag[i]=0;
       }
  return false;
}


main()
{
 cout<<"输入给定的数字个数 \n";
 cin>>n;
 for (int p=0;p<n;p++)
 {
  cout<<"输入第"<<p+1<<"个数字 \n";
  cin>>a[p];
  flag[p]=0;
 }
 cout<<"输入要求的数字 \n";
 cin>>m;

 for(k=0;k<n;k++)
  if(search(0))
  {
   cout<<k<<"\n";
   //out();
   return;
  }
 cout<<"No Solution! \n";
}

  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是回溯法求解无优先级运算问题的C语言代码和注释: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> // 定义运算符和数字的最大数量 #define MAX_OPERATORS 5 #define MAX_NUMBERS 6 // 定义运算符和数字的数组 char operators[MAX_OPERATORS] = {'+', '-', '*', '/', '%'}; int numbers[MAX_NUMBERS]; // 定义已经使用过的运算符和数字的数组 bool used_operators[MAX_OPERATORS]; bool used_numbers[MAX_NUMBERS]; // 定义目标数字和当前结果 int target_number; int current_result; // 定义回溯函数 bool backtrack(int depth) { // 如果深度已经达到了数字的最大数量,判断当前结果是否等于目标数字 if (depth == MAX_NUMBERS) { return current_result == target_number; } // 遍历所有未使用过的数字 for (int i = 0; i < MAX_NUMBERS; i++) { if (!used_numbers[i]) { used_numbers[i] = true; current_result += numbers[i]; // 递归调用回溯函数 if (backtrack(depth + 1)) { return true; } current_result -= numbers[i]; used_numbers[i] = false; } } // 遍历所有未使用过的运算符 for (int i = 0; i < MAX_OPERATORS; i++) { if (!used_operators[i]) { used_operators[i] = true; // 根据运算符计算当前结果 switch (operators[i]) { case '+': current_result += numbers[depth]; break; case '-': current_result -= numbers[depth]; break; case '*': current_result *= numbers[depth]; break; case '/': // 如果除数为0,跳过 if (numbers[depth] == 0) { break; } current_result /= numbers[depth]; break; case '%': // 如果除数为0,跳过 if (numbers[depth] == 0) { break; } current_result %= numbers[depth]; break; } // 递归调用回溯函数 if (backtrack(depth + 1)) { return true; } // 恢复当前结果和使用标记 switch (operators[i]) { case '+': current_result -= numbers[depth]; break; case '-': current_result += numbers[depth]; break; case '*': current_result /= numbers[depth]; break; case '/': current_result *= numbers[depth]; break; case '%': current_result *= numbers[depth]; break; } used_operators[i] = false; } } return false; } int main() { // 输入目标数字和数字的数量 printf("Please enter the target number: "); scanf("%d", &target_number); printf("Please enter the number of numbers: "); int n; scanf("%d", &n); // 输入数字 printf("Please enter %d numbers: ", n); for (int i = 0; i < n; i++) { scanf("%d", &numbers[i]); } // 初始化使用标记 for (int i = 0; i < MAX_OPERATORS; i++) { used_operators[i] = false; } for (int i = 0; i < MAX_NUMBERS; i++) { used_numbers[i] = false; } // 调用回溯函数 if (backtrack(0)) { printf("Found a solution!\n"); } else { printf("No solution found.\n"); } return 0; } ``` 注释中已经详细解释了代码的每个部分,这里不再赘述。需要注意的是,这个代码中假设了运算符和数字的数量是固定的,如果需要处理不同数量的情况,需要动态分配数组并修改相应的常量。此外,代码中使用了除法和取模运算符,需要特别注意除数为0的情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值